// pages/shop/shop.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
    data: {
        goods:[],
        navList:[],
        imgUrls: [],
        indicatorDots: true,
        vertical: false,
        autoplay: true,
        circular: true,
        interval: 4000,
        duration: 500,
        previousMargin: 0,
        nextMargin: 0,
        indicatorColor:"raba(255,255,255,.3)",
        indicatorActiveColor:"#ffffff",
        imgWidth:wx.getSystemInfoSync().windowWidth,
        currentTab:0,
        isGoodsNull:''
    },

    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
        var that = this;
        utils.sendAjax(app.globalData.apiUrl+'/get_banner','GET',this.getBanner);
        utils.sendAjax(app.globalData.apiUrl+'/get_shop_class','GET',this.getShopClass);
        utils.sendAjax(app.globalData.apiUrl+'/get_shop_content','GET',this.getShopContent);
  },
    /*
     *  ajax success function
     */

    //获取banner图callback
   getBanner(res){
      var that = this;
       that.setData({
          imgUrls:res.data
      })
    },
    //获取商品分类
    getShopClass(res){
      var that = this;
        that.setData({
          navList:res.data
      })
    },
    //获取商品列表
    getShopContent(res){
        console.log(res.data);
        var that = this;
        that.setData({
            goods:res.data
        })
        that.getContainerH();
    },


    /*
     * 功能性函数
     */
    //获取swiper 图片高度
    imgHeight:function(e){
        var winW = wx.getSystemInfoSync().windowWidth;
        var imgW = e.detail.width;
        var imgH = e.detail.height;
        var swiperH = winW*imgH/imgW+"px";
        this.setData({
            Height:swiperH
        })
    },
    //自适应swiper高度
    getContainerH:function(){
        let that = this;
        let currTab = that.data.currentTab;
        if(that.data.goods != ''){
            let curr = that.data.goods[currTab].content.length;
            wx.createSelectorQuery().select('.goods-item').boundingClientRect(function(res){
                var resultLen = Math.ceil(curr/2);
                console.log(res);
                that.setData({
                    swiperH:(res.height+10)*resultLen+20,
                    isGoodsNull:false
                })
            }).exec()
        }else{
            that.setData({
                isGoodsNull:true
            })
        }
    },
    //选择导航栏
    switchNav(event){
        var cur = event.currentTarget.dataset.current;
        if (this.data.currentTab == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
        this.getContainerH();
    },
    //切换导航栏
    switchTab(event){
        var cur = event.detail.current;
        this.setData({
            currentTab: cur
        });
        this.getContainerH();
    },

    /*
     * 方法
     */
    //跳的详情页
    jumpToDetails:function(option){
        var id = option.currentTarget.dataset.id;
        wx.navigateTo({
            url:"/subPages/shopPage/details/index?id="+id
        })
    },
    //购物车页面
    toShoppingCart(){
        wx.navigateTo({
            url:'/subPages/shopPage/shoppingCart/shoppingCart'
        })
    },

    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
